import 'package:bloctest/pages/TabPage.dart';
import 'package:bloctest/pages/login_page/login_presenter.dart';
import 'package:bloctest/utils/base/LifeCyclePage.dart';
import 'package:bloctest/utils/loading.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:oktoast/oktoast.dart';

import '../../utils/RouteUtils.dart';
import '../../utils/Routes.dart';

class LoginPage extends LifeCyclePage {
  const LoginPage({super.key});

  @override
  LoginPageState createState() => LoginPageState();
}

class LoginPageState extends LifeCyclePageState<LoginPage> {
  TextEditingController? _nameController;
  FocusNode? _nameFocusNode;

  TextEditingController? _passController;
  FocusNode? _passFocusNode;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _nameController = TextEditingController();
    _nameFocusNode = FocusNode();
    _passController = TextEditingController();
    _passFocusNode = FocusNode();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Expanded(
        child: Container(
          color: Colors.teal,
          padding: EdgeInsets.only(left: 10.r, right: 10.r),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              inputWidget(_nameController!, _nameFocusNode!, "请输入账户"),
              SizedBox(
                height: 10.h,
              ),
              inputWidget(_passController!, _passFocusNode!, "请输入密码",
                  obscureText: true),
              SizedBox(
                height: 30.h,
              ),
              InkWell(
                  onTap: () {
                    doLogin();
                  },
                  child: Container(
                    width: 200.w,
                    padding: EdgeInsets.all(20.r),
                    decoration: BoxDecoration(
                        border: Border.all(width: 1.sp, color: Colors.white),
                        borderRadius: BorderRadius.all(Radius.circular(10.r))),
                    child: Text(
                      "开始登录",
                      style: TextStyle(color: Colors.white, fontSize: 14.sp),
                      textAlign: TextAlign.center,
                    ),
                  ))
            ],
          ),
        ),
      ),
    ) ;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _nameController?.dispose();
    _nameController = null;
    _passController?.dispose();
    _passController = null;
    _nameFocusNode?.dispose();
    _nameFocusNode = null;
    _passFocusNode?.dispose();
    _passFocusNode = null;

    super.dispose();
  }

  @override
  LoginPagePresenter createPresenter() => LoginPagePresenter(this);

  @override
  // TODO: implement presenter
  LoginPagePresenter get presenter => super.presenter as LoginPagePresenter;

  doLogin() {
    if (_nameController!.text.isEmpty) {
      showToast("请输入账号");
    } else if (_passController!.text.isEmpty) {
      showToast("请输入密码");
    } else {
      Loading.showLoading();
      presenter
          .loginAction(_nameController!.text, _passController!.text)
          .then((value) => RouteUtils.pushAndRemoveUntil(context, TabPage()));
    }
  }
}

Widget inputWidget(
    TextEditingController controller, FocusNode focusNode, String? labText,
    {bool? obscureText}) {
  return TextField(
    style: TextStyle(color: Colors.white),
    obscureText: obscureText ?? false,
    controller: controller,
    focusNode: focusNode,
    decoration: InputDecoration(
        labelStyle: TextStyle(color: Colors.white),
        labelText: labText ?? "请输入",
        enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.white, width: 0.5.r),
            borderRadius: BorderRadius.all(Radius.circular(10.r))),
        focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.white, width: 2.r),
            borderRadius: BorderRadius.all(Radius.circular(10.r)))),
  );
}
